package net.cathackers.devel.scmp.auth.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import net.cathackers.devel.scmp.auth.exceptions.ProfileAlreadyExistsException;
import net.cathackers.devel.scmp.auth.exceptions.ProfileNotFoundException;
import net.cathackers.devel.scmp.auth.interfaces.IdentityManager;
import net.cathackers.devel.scmp.auth.interfaces.UserProfile;
import net.cathackers.devel.scmp.auth.interfaces.UserProfileField;
import net.cathackers.devel.scmp.exceptions.BackendException;

public class LocalUserProfile implements UserProfile {

    private IdentityManager backend;
    private boolean _isDirty_;
    private Map<UserProfileField, String> valuesOverlay;
//    private Map<UserProfileListField, List<String>> listFieldsOverlay;
    
    
    public LocalUserProfile(String id, IdentityManager backend) {
	this.backend = backend;
	valuesOverlay = Collections
		.synchronizedMap(new HashMap<UserProfileField, String>());
//	listFieldsOverlay = Collections
//		.synchronizedMap(new HashMap<UserProfileListField , List<String>>());
	this.valuesOverlay.put(UserProfileField.ID, id);
    }
    
    @Override
    public boolean isDirty() {
	return _isDirty_;
    }

    @Override
    public String getID() {
	return valuesOverlay.get(UserProfileField.ID);
    }

    

    @Override
    public String[] getAllFieldNames() {
	String[] retArray = new String[valuesOverlay.size()];
	UserProfileField[] keySetTemp = null;
	keySetTemp = valuesOverlay.keySet().toArray(new UserProfileField[valuesOverlay.size()]);
	for(int i = 0; i < keySetTemp.length; i++)
	{
	    retArray[i] = keySetTemp[i].name();
	}
	return retArray;
    }
  

    

    @Override
    public String getFieldValue(UserProfileField field) throws ProfileNotFoundException,BackendException {
	String retValue = "";
	if(valuesOverlay.containsKey(field))
	{
	    retValue = this.valuesOverlay.get(field);
	}
	else
	{
	    retValue = backend.getField(this, field);
	    this.valuesOverlay.put(field, retValue);
	}
	return retValue;
    }

    @Override
    public boolean setField(UserProfileField field, String value) {
	boolean ret = false;
	
	this.valuesOverlay.put(field, value);
	ret = true;
	_isDirty_ = true;
	return ret;
    }

    @Override
    public void syncObject() throws ProfileNotFoundException, BackendException{
	if(isDirty())
	   this.backend.updateProfile(this);
	}
    
    
    @Override
    public void addProfileAsNew() throws  ProfileAlreadyExistsException, BackendException{
	backend.addProfile(this);
    }
    
    //TODO edit the access modifier for the 2 next functions, I still don't get  the scopes of java packages
    // NOTE I don't think we need to set the ID as it's a read-only value.
    // User shouldn't change his ID, I suggest you comment those out unless you have a good reason.
    public void setID(String id){
	this.valuesOverlay.put(UserProfileField.ID, id);
    }
    public void setBackend(IdentityManager backend){
	this.backend = backend;
    }
    
    
    /* @Override
    public void appendToListField(UserProfileField field, String value) {
	if(this.listFieldsOverlay.containsKey(field))
	{
	    this.listFieldsOverlay.get(field).add(value);
	}
    }
    
    @Override
    public String[] getListFieldValue(UserProfileListField field) throws ProfileNotFoundException,BackendConnectException{
	String[] retArr = null;
	if(this.listFieldsOverlay.containsKey(field)){
	    this.listFieldsOverlay.get(field).toArray(retArr);
	}
	else{
	    retArr = backend.getListField(this, field);
	    this.listFieldsOverlay.put(field, Arrays.asList(retArr));
	}
	return retArr;
    }
    
     @Override
     public String[] getAllListFieldNames() {
 	String[] retArray = new String[listFieldsOverlay.size()];
 	this.listFieldsOverlay.keySet().toArray(retArray);
 	return retArray;
     }
     
     
     public void setListField(UserProfileListField field, List<String> values) {
	    this.listFieldsOverlay.put(field, values);
	    _isDirty_ = true;
    }
    
    @Override
    public void setListField(UserProfileListField field, String oldValue, String newValue) {
	if(listFieldsOverlay.containsKey(field))
	if(this.listFieldsOverlay.containsKey(field))
	{
	    List<String> listField = this.listFieldsOverlay.get(field);
	    int index = listField.indexOf(oldValue);
	    if(index != -1)
	    {
		listField.set(index, newValue);
		_isDirty_ = true;
	    }
	    
	}
    }
     */

}
